package com.heima.wemedia.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChanelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.service.WmChannelService;
import com.heima.wemedia.mapper.WmChannelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 马毅爽
* @description 针对表【wm_channel(频道信息表)】的数据库操作Service实现
* @createDate 2023-12-03 20:24:44
*/
@Service
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel>
    implements WmChannelService{

    @Override
    public ResponseResult findAll() {
        List<WmChannel> list = list();
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult listByDto(WmChanelDto dto) {
        // 1. 检查参数
        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 获取分页参数
        Page<WmChannel> p = new Page<WmChannel>(dto.getPage(), dto.getSize()).addOrder(new OrderItem("created_time", false));
        // 3. 条件查询
        Page<WmChannel> page = lambdaQuery()
                .like(dto.getName() != null, WmChannel::getName, dto.getName())
                .page(p);
        // 4. 返回数据
        ResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), (int)page.getTotal());

        return result.ok(page.getRecords());
    }

    @Override
    @Transactional
    public ResponseResult saveOrUpdateOne(WmChannel channel) {
        // 1. 检查参数
        if (channel == null || StrUtil.isBlank(channel.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 判断是否重复
        if (channel.getId() == null){
            WmChannel one = lambdaQuery()
                    .eq(WmChannel::getName, channel.getName())
                    .one();
            if (one != null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }
        }
        // 3. 补全参数
        channel.setCreatedTime(new Date());
        if (channel.getOrd() == null){
            channel.setOrd(10);
        }
        if (channel.getIsDefault() == null){
            channel.setIsDefault(true);
        }
        // 4. 保存或修改
        saveOrUpdate(channel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        // 1. 检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 检查是否禁用
        WmChannel channel = lambdaQuery().eq(WmChannel::getId, id).one();
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_IS_ENABLE);
        }
        // 3. 删除频道
        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}




